iT邦幫忙

2024 iThome 鐵人賽

DAY 14
0
Software Development

Unity黑科技揭秘:30個專業遊戲開發者必知的開發技巧系列 第 14

Unity Windows - 使用Windows API製作桌面小精靈 🧚

  • 分享至 

  • xImage
  •  

廢話不多說:

using UnityEngine;
using System;
using System.Runtime.InteropServices;

public class TransparentWindow : MonoBehaviour
{
    [DllImport("user32.dll")]
    private static extern IntPtr GetActiveWindow();

    [DllImport("user32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

    [DllImport("user32.dll")]
    private static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);

    private const int GWL_EXSTYLE = -20;
    private const uint WS_EX_LAYERED = 0x00080000;
    private const uint WS_EX_TRANSPARENT = 0x00000020;
    private const uint LWA_ALPHA = 0x00000002;

    private IntPtr hWnd;
    private Material transparentMaterial;

    private void Start()
    {
        #if UNITY_STANDALONE_WIN
        hWnd = GetActiveWindow();
        uint exStyle = (uint)SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_LAYERED | WS_EX_TRANSPARENT);
        
        if (exStyle == 0)
        {
            Debug.LogError("Failed to set window style. Error: " + Marshal.GetLastWin32Error());
            return;
        }

        if (!SetLayeredWindowAttributes(hWnd, 0, 255, LWA_ALPHA))
        {
            Debug.LogError("Failed to set window attributes. Error: " + Marshal.GetLastWin32Error());
            return;
        }

        SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, 0x0002 | 0x0001 | 0x0004);
        
        transparentMaterial = new Material(Shader.Find("Unlit/Transparent"));
        #else
        Debug.LogWarning("TransparentWindow is only supported on Windows standalone builds.");
        #endif
    }

    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        #if UNITY_STANDALONE_WIN
        if (transparentMaterial != null)
        {
            Graphics.Blit(source, destination, transparentMaterial);
        }
        else
        {
            Graphics.Blit(source, destination);
        }
        #else
        Graphics.Blit(source, destination);
        #endif
    }

    private void OnDestroy()
    {
        if (transparentMaterial != null)
        {
            Destroy(transparentMaterial);
        }
    }
}

使用步驟:

  1. 創建新的Unity Project
  2. 創建一個新的C#腳本,命名為"TransparentWindow"
  3. 將上述代碼複製到腳本中。
  4. 將TransparentWindow腳本附加到GameObject上。
  5. 確保場景中有一個Camera組件,TransparentWindow腳本將影響這個Camera的渲染。
  6. 在Build Settings中,選擇"PC, Mac & Linux Standalone"平台。
  7. 在Player Settings中,將"Fullscreen Mode"設置為"Windowed"。

上一篇
Unity OpenUPM - 在GitHub 託管架設自己的Unity插件
下一篇
Unity Rider - 最好用的Unity Code編輯器!
系列文
Unity黑科技揭秘:30個專業遊戲開發者必知的開發技巧25
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言